const app = getApp()


const db = wx.cloud.database().collection("certificate")
let username = ""
let user手机号 = ""
let carnumber = ""
let 角色 = ""
Page({
  data: {
    
    PlaceHolderImages: ['/images/audit-1.png', '/images/audit-2.png', '/images/audit-3.png'],
    角色: ['老师', '学生', '校外人员','家长'],
    activeIndex: 0,
    readOnly: false,
    username:'',
    user手机号:'',
    carnumber:'',
    

    

  },
  




  onLoad(options) {
    $init(this)
    publishCertificate.get_one_certificate(db,{},this)
  },
  chooseImage(e) {
    if (this.data.readOnly) { return; }
    let that = this;
    wx.chooseImage({
      count: 1,  //最多可以选择的图片总数
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log('选择成功',res)
        that.uploadImg(res.tempFilePaths[0]);  
        
      }  
    })    
  },
  uploadImg(fileUrl){
    wx.cloud.uploadFile({
      cloudPath: 'qqq.png', // 上传至云端的路径
      filePath: fileUrl, // 小程序临时文件路径
      success: res => {
        // 返回文件 ID
        console.log('上传成功',res)
      },
      fail: console.error
    })
    
  },
  
  getForm:function(e){
    var formdata = e.detail.value;
    this.setData({
      "data.username":formdata.username,
      "data.user手机号":formdata.user手机号,
      "data.carnumber":formdata.carnumber,
      "data.角色":formdata.角色
    })
    console.log("更新data",e)
  },
  getData:function(e){
       var getdata = this.data;
       const db = wx.cloud.database();
       db.collection("certificate").add({
           data:{
             username:getdata.data.username,
             user手机号:getdata.data.user手机号,
             carnumber:getdata.data.carnumber,
             角色:getdata.data.角色,
           }
         }).then(res=>{
           console.log("添加至數據庫成功",res)
         }).catch(res=>{
           console.log("添加失敗",res)
         })
       
  },

 
  chaxun(){
    db.get({
      success(res){
        console.log("查询成功",res)
        wx.navigateTo({
          url: '../certificate/chaxun.wxml',
        })
      }
    })
  },
 
  onLoad: function (options) {
  
  },
  onReady: function () {

  }
   
})